package com.ubing.boot.notesforjava.test.juc;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName : fairLock
 * @Description : fairLock
 * @Author : Xxxxx
 * @Date: 2023-03-10 16:32
 */
public class fairLock extends Thread{
    private static ReentrantLock lock= new ReentrantLock(true);
    @Override
    public void run() {
        for(int i=0;i<100;i++){
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"获得锁");
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
//        fairLock fairLock = new fairLock();
//        Thread thread = new Thread(fairLock);
//        Thread thread1 = new Thread(fairLock);
//        thread.start();
//        thread1.start();


        CyclicBarrier cyclicBarrier = new CyclicBarrier(20, () -> {
            System.out.println("20个线程到了，发车");
        });

        for (int i =0;i<100;i++){
            new Thread(()->{
                try {
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();

        }



    }
}
